<?php
declare (strict_types = 1);

namespace app\controller\admin;

use app\event\AdminActionEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use think\Request;
use app\model\AdminConfig;
use app\traits\MyControllerTrait;
use think\facade\Session;
use app\validate\admin\AdminConfigValidate;
use think\exception\ValidateException;

class AdminConfigController
{
    use MyControllerTrait;

    protected string $uri = '/admin/config';

    public function index()
    {
        $res    = AdminConfig::select()->toArray();
        $config = [];
        foreach ($res as $key => $val){
            $config[$val['title']] = $val['content'];
        }

        $data = [
            'uri'   => $this->uri,
            'title' => $this->getPermissionTitle($this->uri.'/'.__FUNCTION__),
            'menus' => $this->getMenuNav($this->uri.'/'.__FUNCTION__),
            'data'  => $config
        ];
        return view($this->uri.'/'.__FUNCTION__, $data);
    }// index() end

    public function edit(Request $request)
    {
        $res    = AdminConfig::select()->toArray();
        $config = [];
        foreach ($res as $key => $val){
            $config[$val['title']] = $val['content'];
        }

        $data = [
            'uri'       => $this->uri,
            'data'      => $config,
            '__token__' => $request->buildToken()
        ];
        return view($this->uri.'/'.__FUNCTION__, $data);
    }// edit() end

    public function update(Request $request)
    {
        $website            = $request->put('website/s', '');// 网站名称
        $bei_an             = $request->put('bei_an/s', '');// 备案号
        $copyright          = $request->put('copyright/s', '');// 版权信息
        $watermark_switch   = $request->put('watermark_switch/d', '0');// 版权水印

        $check_data = [
            'website'           => $website,
            'bei_an'            => $bei_an,
            'copyright'         => $copyright,
            'watermark_switch'  => $watermark_switch,
        ];
        try {
            validate(AdminConfigValidate::class)->check($check_data);
        } catch (ValidateException $validateException) {
            return json(['code' => 0, 'message' => $validateException->getError()]);
        }
        if ($request->checkToken('__token__') === false) return json(['code' => 0, 'message' => '页面失效,请刷新页面']);

        foreach ($check_data as $title => $content)
        {
            $res = AdminConfig::whereRaw('title = :title', ['title' => $title])->findOrEmpty();
            if ($res->isEmpty()) continue;

            $res->content = $content;
            $res->save();
        }

        // 更新 session 里的 config
        Session::set('config.watermark_switch', $watermark_switch);
        Session::set('config.copyright', $copyright);

        $data = [
            $this->getPermissionTitle($this->uri.'/'.__FUNCTION__),
            Session::get("admin_user.account"),
            $check_data
        ];
        (new EventDispatcher)->dispatch((new AdminActionEvent($request, $data)), AdminActionEvent::EVENT_NAME);
        return json(['code' => 1, 'message' => '保存成功']);
    }// update() end
}
